/*
** event_func.c for Event function in /home/vriely/Dropbox/work/Unix/Zappy_rendu/serv-src
**
** Made by Samuel Vrielynck
** Login   <vriely_s@epitech.net>
**
** Started on  Tue Jun 26 05:12:04 2012 Samuel Vrielynck
** Last update Sun Jul 15 20:38:45 2012 Samuel Vrielynck
*/

#include			<stdlib.h>

#include			"server.h"

static const t_handle_event	tab_event[] =
{
  { FORK, &handle_fork },
  { INCATATION, &handle_inca },
  { BROADCAST, &handle_broadcast },
  { EXPULSE, &handle_expulse },
};

void				check_event(t_list *c, t_game *game,
    long unsigned int elapsed_tim)
{
  t_event			*event;
  t_item			*cur;

  if (game->event->len > 0)
  {
    cur = game->event->head;
    while (cur != NULL)
    {
      event = (struct s_event *)cur->content;
      if (event->action < elapsed_tim)
      {
	if (tab_event[event->type].func(event, game, c, elapsed_tim) != 0)
	  cur = game->event->erase(game->event, cur , &free);
      }
      else
	event->action -= elapsed_tim;
      if (cur)
      {
	event = (struct s_event *)cur->content;
	getsleeptime(&(game->sleep), event->action);
      }
      if (cur)
	cur = cur->next;
    }
  }
}

char				is_alive(unsigned int time, t_gamer gamer,
    long unsigned int elapsed_time)
{
  while (elapsed_time > gamer.food)
  {
    elapsed_time -= gamer.food;
    gamer.food = FOOD_TIMER(time);
    gamer.inventory[FOOD]--;
  }
  if (gamer.inventory[FOOD] < 0)
    return (0);
  return (1);
}
